/**
 * @Title: LeaveOutAdapter.java
 * @Package: com.jason.bluetooth.fingerprint.adapter
 * @Descripton: TODO
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年11月9日23:32:04
 * @Version: V1.0
 */
package com.jason.platform.supervision.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.hzlj.supervision.changsha.R;
import com.jason.platform.supervision.activity.BaiduMapActivity;
import com.jason.platform.supervision.activity.MonitorActivity;
import com.jason.platform.supervision.beans.LocateBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: MonitorchildAdapter
 * @Description: 统计分析——走访记录 适配器
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-7-29 14:56:48
 */
public class MonitorchildAdapter extends BaseAdapter {

	private static final String TAG = MonitorchildAdapter.class.getSimpleName();

	private Context mContext;
	private LayoutInflater mLayoutInflater;
	private List<LocateBean> mDatas = new ArrayList<LocateBean>();
	public boolean isLoading = false;

	public void setIsLoading(boolean flag){
		this.isLoading = flag;
	}

	public MonitorchildAdapter(Context context) {
		super();
		this.mContext = context;
		this.mLayoutInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mDatas = new ArrayList<LocateBean>();
	}

	@Override
	public int getCount() {
		return mDatas.size();
	}

	@Override
	public Object getItem(int position) {
		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void addAll(List<LocateBean> datas) {
		this.mDatas = datas;
		if (mDatas == null){
			mDatas = new ArrayList<LocateBean>();
		}
		notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = null;
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.adapter_monitor_child, null);
			holder = new Holder();
			holder.mNameTextView = (TextView) convertView
					.findViewById(R.id.monitor_child_name);
			holder.mDateTextView = (TextView) convertView
					.findViewById(R.id.monitor_child_date);
			holder.mOperateTextView = (TextView) convertView
					.findViewById(R.id.monitor_child_operate);
			holder.mLocateTextView = (TextView) convertView.findViewById(R.id.monitor_child_locate);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		final LocateBean bean = (LocateBean) getItem(position);
		if (bean != null){
			holder.mNameTextView.setText(bean.getElectronicsName());
			holder.mDateTextView.setText(bean.getPositionTime());
		}

		//TODO 填充数据
		holder.mOperateTextView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//进入地图
				if (bean.isOpenMap()){
					Intent intent = new Intent(mContext, BaiduMapActivity.class);
					intent.setAction(BaiduMapActivity.ACTION_LOCATE_LOOK);
					intent.putExtra(BaiduMapActivity.KEY_LOCATE_LOOK,bean);
					mContext.startActivity(intent);
				}else {
					Toast.makeText(mContext, "暂无坐标信息", Toast.LENGTH_SHORT).show();
				}
			}
		});

		holder.mLocateTextView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!MonitorActivity.isLoading){
					MonitorActivity.isLoading = true;
					((MonitorActivity)mContext).getLocate(bean);
				}else {
					Toast.makeText(mContext,"请稍等，正在获取位置信息...",Toast.LENGTH_SHORT).show();
				}
			}
		});
		return convertView;
	}

	class Holder {
		TextView mNameTextView;
		TextView mDateTextView;
		TextView mOperateTextView;
		TextView mLocateTextView;
	}

}
